<script>	
	import LocalDAO from 'common/localDAO'
	import RemoteDAO from 'common/remoteDAO'
	import LoadMore from 'components/LoadMore'
	import CommonUtil from 'common/common.util'
	
	 export default {
    	data () {
    		return {
    			list:[],
    			pageNum : 0,
    			pageSize : 20,
    			gameId : "",
    			errorMessage : "error",
    			showError : false,
    			hasmore : true,
    			bottomPullText:"向上滑动加载更多"
    		}
    		
      },
       components:{
      		'gj-loadmore':LoadMore
    	 },
       created () {
       		this.$store.dispatch('setLoading', {visible: true});            
    	 },
    	methods: {
    	    fetchData(afterLoaded){
    	    	let self = this;
            let userCode = this.$route.query.userCode || "";
            self.gameId = this.$route.query.gameId || "";
        		RemoteDAO.callJCL11124(userCode,self.gameId,self.pageNum,self.pageSize,data => {
        			self.$store.dispatch('setLoading', {visible: false});
            		if(data.error === "0"){
                		let dataList = {};
                		dataList.list = data.data.body.holders;
                		if(dataList.list.length > 0){
                    		let list = [];
                    		_.each(dataList.list, function (item, index) {
                        		let object = {};
                        		object.stock_name = item.stock_name;
                        		object.stock_code = item.stock_code;
                        		if(item.exchange_type === 1 || item.exchange_type == "1"){
                        			object.exchange_type = "SH";
                        		}else{
                        			object.exchange_type = "SZ";
                        		}
                        		object.keep_cost_price = item.keep_cost_price;
                        		object.last_price = item.last_price; //现价
                        		object.current_vol = item.current_vol; // 持仓数量
                        		object.income_balance = item.income_balance; // 持仓数量
                        		object.position = item.position; // 持仓数量
                        		if(item.keep_cost_price>0){
                            		object.yieldRate = CommonUtil.toFixed((item.last_price - item.keep_cost_price) / item.keep_cost_price * 100, 2);
                        		}else{
                            		object.yieldRate ="--";
                        		}                        		

                        		list.push(object);
                   	    	});
                        	self.list = self.list.concat(list);
                    		//增加一个分页
                    		if(dataList.list.length === self.pageSize){
                        		self.pageNum = self.pageNum + self.pageSize;
                        		self.hasmore = true;
                        		self.hasInit = true;
                    		}else{
                        		self.hasmore = false;
                    		}
                    		afterLoaded(self.hasmore);
                		}else{
                			if(self.pageNum === 0){
                			   self.showError = true;
                			   self.errorMessage = "大王，他还没有持仓哦！";
                			   self.hasmore = 'error';
                			}else{
                				self.hasmore = false;
                			}
                			afterLoaded(self.hasmore);
                		}
            		}else{
            			  self.hasmore = false;
            			  afterLoaded("error");
            			  self.showError = true;
                		self.errorMessage = data.data;
            		}
        		});
    	    },
    	    loadBottom() {
        		let vm = this;
        		vm.fetchData(hasmore => {
        	    	if(hasmore === "error"){
        	    		vm.$refs.loadmore.onBottomError();
        	    	}else if(!hasmore){
            			vm.$refs.loadmore.onBottomAllLoaded();//全部数据加载完成
          			} else {
            			vm.$refs.loadmore.onBottomLoaded(); //单次加载成功
          			}
        		});
      		},
      		toQuotation(item){
      		  const stockCode = item.exchange_type + item.stock_code;
          	  RemoteDAO.callStockLive(stockCode,"game","1&isAttendGame=1&gameId=" + this.gameId);
      		}
    	},
    	destroyed(){
          this.$store.dispatch('setLoading', {visible: false});
        }
    }
</script>
